#ifndef Ai_Message_h__
#define Ai_Message_h__

#include <set>
#include <vector>

namespace MessageSystem
{

enum MESSAGE_TYPE
{
    GO_HERE,
    TARGET_THIS,
    RELOAD_TEXTURES
};

struct Ai_Message
{
    Ai_Message(MESSAGE_TYPE type, float time) : 
    m_type(type), m_dispatchTime(time), m_data(NULL)
    {}

    MESSAGE_TYPE m_type;
    float m_dispatchTime;

    void* m_data;
};
struct Message_Compare
{
    bool operator() (const Ai_Message& lhs,const Ai_Message& rhs )
    {
        return lhs.m_dispatchTime < rhs.m_dispatchTime;
    }
};

typedef std::vector<Ai_Message> Messages;
typedef std::set<Ai_Message,Message_Compare> MessageSet;

class Message_Handler
{
public:
    virtual ~Message_Handler() = 0 {}
    virtual void vProcessInbox() = 0;
    virtual void vReceiveMessage(Ai_Message msg) = 0;
    virtual void vSendMessage(Ai_Message msg) = 0;
    virtual Messages& vGetOutBox() = 0;
};

class Message_Dispatcher
{
public:
    Message_Dispatcher() : m_timeStamp(0.0f) {}
    void Update(float dt);
    void RegisterHandler(Message_Handler* handler);
    void UnregisterHandler(Message_Handler* handler);
    void UnregisterAll();
    void ForceMessage(Ai_Message msg);
private:
    void ProcessHandlers();
    void ProcessMessages(float dt);

private:
    float m_timeStamp;
    MessageSet m_messages;
    std::vector<Message_Handler*> m_handlers;
};

};//MessageSystem

#endif // Ai_Message_h__
